﻿// ------------------------------------------------------------------------------
// <copyright file="ColorGenerator.cs" company="skumaronline.com">
//    Copyright (C) 2012 Suresh Kumar T.  All rights reserved.
// </copyright>
// ------------------------------------------------------------------------------

namespace StoreAppLib.UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using StoreAppLib.UI;
    using Windows.UI;

    /// <summary>
    /// Color generator
    /// </summary>
    public class ColorGenerator
    {
        /// <summary>
        /// Holds hues
        /// </summary>
        private List<double> hues;

        /// <summary>
        /// Holds saturation and brightness
        /// </summary>
        private List<SB> sb;

        /// <summary>
        /// Holds hue index
        /// </summary>
        private int hueIndex;

        /// <summary>
        /// Saturation and brightness index
        /// </summary>
        private int saturationBrightnessIndex;

        /// <summary>
        /// Initializes a new instance of the <see cref="ColorGenerator"/> class"
        /// </summary>
        public ColorGenerator()
        {
            this.hueIndex = 0;
            this.saturationBrightnessIndex = 0;

            this.hues = new List<double>();
            this.sb = new List<SB>();

            for (double h = 0; h < 360; h += 60)
            {
                this.hues.Add(h);
            }

            for (double h = 15; h < 360; h += 60)
            {
                this.hues.Add(h);
            }

            this.sb.Add(new SB() { Saturation = .7, Brightness = .9 });
            this.sb.Add(new SB() { Saturation = .9, Brightness = .7 });

            this.sb.Add(new SB() { Saturation = .6, Brightness = .93 });
            this.sb.Add(new SB() { Saturation = .93, Brightness = .6 });

            this.sb.Add(new SB() { Saturation = .5, Brightness = .95 });
            this.sb.Add(new SB() { Saturation = .95, Brightness = .5 });

            this.sb.Add(new SB() { Saturation = .7, Brightness = .8 });
            this.sb.Add(new SB() { Saturation = .8, Brightness = .7 });

            this.sb.Add(new SB() { Saturation = .6, Brightness = .8 });
            this.sb.Add(new SB() { Saturation = .8, Brightness = .6 });

            this.sb.Add(new SB() { Saturation = .5, Brightness = .8 });
            this.sb.Add(new SB() { Saturation = .8, Brightness = .5 });

            this.sb.Add(new SB() { Saturation = .5, Brightness = .7 });
            this.sb.Add(new SB() { Saturation = .7, Brightness = .5 });

            this.sb.Add(new SB() { Saturation = .5, Brightness = .6 });
            this.sb.Add(new SB() { Saturation = .6, Brightness = .5 });
        }

        /// <summary>
        /// Gets next color
        /// </summary>
        /// <returns>Color value</returns>
        public Windows.UI.Color GetNextColor()
        {
            if (this.hueIndex >= this.hues.Count || this.saturationBrightnessIndex >= this.sb.Count)
            {
                return Windows.UI.Colors.Transparent;
            }

            HSB hsb = HSB.FromHSB(this.hues[this.hueIndex], this.sb[this.saturationBrightnessIndex].Saturation, this.sb[this.saturationBrightnessIndex].Brightness);

            this.hueIndex = this.hueIndex + 1 < this.hues.Count ? this.hueIndex + 1 : 0;

            if (this.hueIndex == 0)
            {
                this.saturationBrightnessIndex++;
            }

            return hsb.ToColor();
        }

        /// <summary>
        /// Structure to store saturation and brightness
        /// </summary>
        private struct SB
        {
            /// <summary>
            /// Holds saturation
            /// </summary>
            public double Saturation;

            /// <summary>
            /// Holds brightness
            /// </summary>
            public double Brightness;
        }
    }
}
